<template>
	<el-dialog
		:title="showDetail ? $t('agv.taskDetails') : $t('agv.createTasks')"
		v-model="visible"
		:width="showDetail ? '800' : '1287'"
		draggable
		:close-on-click-modal="false"
		style="font-size: large"
	>
		<el-form
			ref="dataFormRef"
			:model="form"
			:rules="dataRules"
			formDialogRef
			v-loading="loading"
			style="margin-bottom: 30px"
			size="default"
			v-if="!showDetail"
		>
			<el-row :gutter="24">
				<el-col :span="8">
					<el-form-item :label="$t('agv.taskType')" prop="businessTaskType" label-width="auto">
						<el-select
							:placeholder="$t('agv.taskType')"
							filterable
							clearable
							remote
							@change="getTaskTypeClick"
							:remote-method="getProcessList"
							v-model="form.businessTaskType"
						>
							<el-option v-for="item in processList" :key="item.itemValue" :label="item.itemValue" :value="item">
								<span style="float: left">{{ item.itemText }}</span>
								<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px"> {{ item.itemValue }} </span>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item :label="$t('agv.priority')" prop="initPriority" label-width="auto">
						<el-select v-model="form.initPriority" @change="select" clearable>
							<el-option v-for="item in priorityList" :key="item.id" :label="item.id" :value="item.name" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item :label="$t('agv.scanType')" prop="scanType" label-width="auto">
						<el-select v-model="form.scanType" @change="select" clearable>
							<el-option v-for="item in scanTypeList" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-card style="max-width: 700px; margin-left: 15px" class="box-card" v-if="showDetail">
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.taskNo') }}：</el-text>
					<span>{{ detailData.taskCode }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col>
					<el-text>{{ $t('agv.taskType') }}：</el-text>
					<span>{{ detailData.businessTaskTypeName }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.scanType') }}：</el-text>
					<span>{{ detailData.scanType }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.agvNo') }}：</el-text>
					<span>{{ detailData.robotCode }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.palletNo') }}：</el-text>
					<span>{{ detailData.cartonNo }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.palletType') }}：</el-text>
					<span>{{ detailData.cartonType }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.transportedQty') }}：</el-text>
					<span>{{ detailData.qty }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.priority') }}：</el-text>
					<span>{{ detailData.initPriority }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.pick-upLocation') }}：</el-text>
					<span>{{ detailData.fromLocationName }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.unloadingPosition') }}：</el-text>
					<span>{{ detailData.toLocationName }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.unloadingPositionQty') }}：</el-text>
					<span>{{ detailData.existQty }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.materialCode') }}：</el-text>
					<span>{{ detailData.materialCode }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.materialName') }}：</el-text>
					<span>{{ detailData.materialName }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-text>{{ $t('agv.creationTime') }}：</el-text>
					<span>{{ detailData.createTime }}</span></el-col
				>
				<el-col :span="12">
					<el-text>{{ $t('agv.completionTime') }}：</el-text>
					<span>{{ detailData.taskCompleteTime }}</span></el-col
				>
			</el-row>
			<el-row :gutter="20" v-if="isWait">
				<el-col :span="20" style="text-align: center; margin-top: 5px">
					<el-button size="default" style="color: #fff; background-color: #c8c9cc" @click="handleCancel">
						{{ $t('agv.cancelTask') }}
					</el-button>
					<el-button size="default" style="color: #fff; background-color: #e6a23c" @click="handleLocation"
						>{{ $t('agv.changDestination') }}
					</el-button>
					<el-button size="default" style="color: #fff; background-color: #c3ac33" @click="handleChangePriority"
						>{{ $t('agv.changePriority') }}
					</el-button>
				</el-col>
			</el-row>
		</el-card>
		<!--创建任务的表格-->
		<el-row>
			<el-col :span="12">
				<el-card class="w-full h-full" shadow="always">
					<template #header>
						<div class="card-header">
							<span>{{$t('agv.operationType')}}{{$t('agv.deliveryGoods')}}</span>
						</div>
					</template>
					<el-form ref="dataFormRef2" :rules="dataRules2" label-width="auto" :label-position="'left'"
						:model="tableList[0]" style="margin-bottom: 30px" size="default">
						<el-form-item :label="$t('agv.process')" prop="processCode" label-width="auto">
							<el-select
								v-model="tableList[0].processCode" @click="getRuleList(tableList[1], '1')" @change="getRuleClick(tableList[1])"
                filterable clearable :remote="true" :disabled="showDetail">
								<el-option v-for="item in perationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.productionLine')" prop="lineCode" label-width="auto">
							<el-select v-model="tableList[0].lineCode" @click="getRuleList(tableList[0], '2')" filterable clearable :remote="true" :disabled="showDetail">
								<el-option v-for="item in tableList[0].lineList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
										{{ item.code }}
									</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.position')" prop="locationCode" label-width="auto">
							<el-select
								v-model="tableList[0].locationCode" @click="getRuleList(tableList[0], '3')" @change="getRuleClick(tableList[0], '3')"
								filterable clearable :disabled="showDetail" :remote="true">
								<el-option v-for="item in tableList[0].locationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.pointPosition')" prop="code" label-width="auto">
							<el-select v-model="tableList[0].code" filterable clearable :remote="true" :disabled="showDetail">
								<el-option v-for="item in tableList[0].locationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.operationType')"  label-width="auto">
							<el-select v-model="tableList[0].operation" clearable :disabled="showDetail">
								<el-option :label="$t('agv.deliveryGoods')" value="COLLECT"></el-option>
								<el-option :label="$t('agv.releaseGoods')" value="DELIVERY"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.goodsType')" prop="wareType" label-width="auto">
							<el-select v-model="tableList[0].wareType" prop="wareType" clearable :disabled="showDetail">
								<el-option v-for="item in createTaskType" :key="item.text" :label="item.text" :value="item.value">
									<span>{{ item.text }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.qty')" prop="qty" label-width="auto">
							<el-input-number v-model="tableList[0].qty" clearable :controls="false" :disabled="showDetail"></el-input-number>
						</el-form-item>
						<el-form-item :label="$t('agv.destinationsQty')" prop="currentQty" label-width="auto">
							<el-input-number v-model="tableList[0].currentQty" clearable :controls="false" :disabled="showDetail"></el-input-number>
						</el-form-item>
					</el-form>
				</el-card>
			</el-col>

			<el-col :span="12">
				<el-card class="w-full h-full" shadow="always">
					<template #header>
						<div class="card-header">
							<span>{{$t('agv.operationType')}}{{$t('agv.releaseGoods')}}</span>
						</div>
					</template>
					<el-form ref="dataFormRef3" :rules="dataRules3" label-width="auto" :label-position="'left'" :model="tableList[1]"
						style="margin-bottom: 30px" size="default">
						<el-form-item :label="$t('agv.process')" prop="processCode" label-width="auto" >
							<el-select v-model="tableList[1].processCode" @click="getRuleList(tableList[1], '1')" :disabled="showDetail"
								@change="getRuleClick(tableList[1])" filterable clearable :remote="true">
								<el-option v-for="item in perationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.productionLine')" prop="lineCode" label-width="auto" >
							<el-select v-model="tableList[1].lineCode" @click="getRuleList(tableList[1], '2')" filterable clearable :remote="true" :disabled="showDetail">
								<el-option v-for="item in tableList[1].lineList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
										{{ item.code }}
									</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.position')" prop="locationCode" label-width="auto" >
							<el-select v-model="tableList[1].locationCode" filterable @click="getRuleList(tableList[1], '3')" :disabled="showDetail"
								@change="getRuleClick(tableList[1], '3')" clearable :remote="true">
								<el-option v-for="item in tableList[1].locationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.pointPosition')" prop="code" label-width="auto" >
							<el-select v-model="tableList[1].code" filterable clearable :remote="true" :disabled="showDetail">
								<el-option v-for="item in tableList[1].locationList" :key="item.id" :label="item.code" :value="item.code">
									<span style="float: left">{{ item.text }}</span>
									<span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">{{ item.code }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.operationType')" label-width="auto" >
							<el-select v-model="tableList[1].operation" clearable :disabled="showDetail">
								<el-option :label="$t('agv.deliveryGoods')" value="COLLECT"></el-option>
								<el-option :label="$t('agv.releaseGoods')" value="DELIVERY"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.goodsType')" prop="wareType" label-width="auto" >
							<el-select v-model="tableList[1].wareType" prop="wareType" clearable :disabled="showDetail">
								<el-option v-for="item in createTaskType" :key="item.text" :label="item.text" :value="item.value">
									<span>{{ item.text }}</span>
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item :label="$t('agv.qty')" prop="qty" label-width="auto" >
							<el-input-number v-model="tableList[1].qty" clearable :controls="false" :disabled="showDetail"></el-input-number>
						</el-form-item>
						<el-form-item :label="$t('agv.destinationsQty')" prop="currentQty" label-width="auto" >
							<el-input-number v-model="tableList[1].currentQty" clearable :controls="false" :disabled="showDetail"></el-input-number>
						</el-form-item>
					</el-form>
				</el-card>
			</el-col>
		</el-row>
		<!--		<el-table-->
		<!--			:data="tableList"-->
		<!--			border-->
		<!--			style="width: 100%; height: 100%"-->
		<!--			:cell-style="{ textAlign: 'center' }"-->
		<!--			:header-cell-style="{ textAlign: 'center' }"-->
		<!--		>-->
		<!--			<el-table-column :label="$t('agv.operation')" v-if="!showDetail" width="100">-->
		<!--				<template #default="scope">-->
		<!--					<el-tag @click="scope.row.edit = true" v-show="!scope.row.edit" type="primary" size="large" :disable-transitions="true"-->
		<!--						>{{ $t('agv.edit') }}-->
		<!--					</el-tag>-->
		<!--					<el-tag @click="scope.row.edit = false" v-show="scope.row.edit" type="info" size="large" :disable-transitions="true"-->
		<!--						>{{ $t('agv.cancel') }}-->
		<!--					</el-tag>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.operation')" v-if="changeLocation" width="100">-->
		<!--				<template #default="scope">-->
		<!--					<el-tag-->
		<!--						@click="-->
		<!--							() => {-->
		<!--								scope.row.operation == 'COLLECT' ? (scope.row.edit = false) : (scope.row.edit = true);-->
		<!--							}-->
		<!--						"-->
		<!--						v-show="!scope.row.edit"-->
		<!--						type="primary"-->
		<!--						size="large"-->
		<!--						:disable-transitions="true"-->
		<!--						>{{ $t('agv.edit') }}-->
		<!--					</el-tag>-->
		<!--					<el-tag @click="scope.row.edit = false" v-show="scope.row.edit" type="info" size="large" :disable-transitions="true"-->
		<!--						>{{ $t('agv.cancel') }}-->
		<!--					</el-tag>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.pointSeq')" v-if="showDetail" prop="seq" width="100" fixed="left"></el-table-column>-->
		<!--			<el-table-column :label="$t('agv.process')" prop="processCode" width="110">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.processCode }}</span>-->
		<!--					<el-select-->
		<!--						prop="processCode"-->
		<!--						v-model="scope.row.processCode"-->
		<!--						@click="getRuleList(scope.row, '1')"-->
		<!--						@change="getRuleClick(scope.row)"-->
		<!--						v-show="scope.row.edit"-->
		<!--						filterable-->
		<!--						clearable-->
		<!--						:remote="true"-->
		<!--					>-->
		<!--						<el-option v-for="item in perationList" :key="item.id" :label="item.code" :value="item.code">-->
		<!--							<span style="float: left">{{ item.text }}</span>-->
		<!--							<span style="float: right; color: var(&#45;&#45;el-text-color-secondary); font-size: 13px">-->
		<!--								{{ item.code }}-->
		<!--							</span>-->
		<!--						</el-option>-->
		<!--					</el-select>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.productionLine')" prop="lineCode" width="200">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.lineCode }}</span>-->
		<!--					<el-select v-model="scope.row.lineCode" @click="getRuleList(scope.row, '2')" v-show="scope.row.edit" filterable clearable :remote="true">-->
		<!--						<el-option v-for="item in scope.row.lineList" :key="item.id" :label="item.code" :value="item.code">-->
		<!--							<span style="float: left">{{ item.text }}</span>-->
		<!--							<span style="float: right; color: var(&#45;&#45;el-text-color-secondary); font-size: 13px">-->
		<!--								{{ item.code }}-->
		<!--							</span>-->
		<!--						</el-option>-->
		<!--					</el-select>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.position')" prop="locationCode" width="200">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.locationCode }}</span>-->
		<!--					<el-select-->
		<!--						v-model="scope.row.locationCode"-->
		<!--						@click="getRuleList(scope.row, '3')"-->
		<!--						@change="getRuleClick(scope.row, '3')"-->
		<!--						v-show="scope.row.edit"-->
		<!--						filterable-->
		<!--						clearable-->
		<!--						:remote="true"-->
		<!--					>-->
		<!--						<el-option v-for="item in scope.row.locationList" :key="item.id" :label="item.code" :value="item.code">-->
		<!--							<span style="float: left">{{ item.text }}</span>-->
		<!--							<span style="float: right; color: var(&#45;&#45;el-text-color-secondary); font-size: 13px">-->
		<!--								{{ item.code }}-->
		<!--							</span>-->
		<!--						</el-option>-->
		<!--					</el-select>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.pointPosition')" prop="code" width="160">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.code }}</span>-->
		<!--					<el-input v-model="scope.row.code" v-show="scope.row.edit" clearable :placeholder="$t('agv.pleaseEnter')"></el-input>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.operationType')" prop="operation" width="120">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.operation }}</span>-->
		<!--					<el-select v-model="scope.row.operation" v-show="scope.row.edit" clearable>-->
		<!--						<el-option :label="$t('agv.deliveryGoods')" value="COLLECT"></el-option>-->
		<!--						<el-option :label="$t('agv.releaseGoods')" value="DELIVERY"></el-option>-->
		<!--					</el-select>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.goodsType')" prop="wareType" width="120">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.wareType }}</span>-->
		<!--					<el-select v-model="scope.row.wareType" prop="wareType" v-show="scope.row.edit" clearable>-->
		<!--						<el-option v-for="item in createTaskType" :key="item.text" :label="item.text" :value="item.value">-->
		<!--							<span>{{ item.text }}</span>-->
		<!--						</el-option>-->
		<!--					</el-select>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.qty')" prop="qty" width="100">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.qty }}</span>-->
		<!--					<el-input-number v-model="scope.row.qty" v-show="scope.row.edit" clearable :controls="false"></el-input-number>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--			<el-table-column :label="$t('agv.destinationsQty')" prop="currentQty" width="105">-->
		<!--				<template #default="scope">-->
		<!--					<span v-show="!scope.row.edit">{{ scope.row.currentQty }}</span>-->
		<!--					<el-input-number v-model="scope.row.currentQty" v-show="scope.row.edit" clearable :controls="false"></el-input-number>-->
		<!--				</template>-->
		<!--			</el-table-column>-->
		<!--		</el-table>-->
		<!--    按钮-->
		<template #footer>
			<div class="dialog-footer">
				<el-button v-if="changeLocation" size="large" @click="handleChange">{{ $t('agv.changDestination') }}</el-button>
				<el-button type="success" v-if="!showDetail" @click="handleCreateClick" size="large">{{ $t('agv.submit') }} </el-button>
				<el-button type="info" @click="handleCancelClick" size="large">{{ $t('agv.cancel') }}</el-button>
			</div>
		</template>
		<changePriority ref="changePriorityRef"></changePriority>
	</el-dialog>
</template>

<script setup lang="ts">
import { cancelTaskByTaskCode, createTask, fetchProcess, fetchTaskDetail, fetchTaskType, modifyTaskLocation } from '/@/api/platform/mes/agv';
import { useMessage } from '/@/hooks/message';
import { useI18n } from 'vue-i18n';
import { i18n } from '/@/i18n';

const { t } = useI18n();

const changePriority = defineAsyncComponent(() => import('./changePriority.vue'));
const getdataList = inject('getdataList');
// 定义变量内容
const dataFormRef = ref();
const dataFormRef2 = ref();
const dataFormRef3 = ref();
const changePriorityRef = ref();
const visible = ref<boolean>(false);
const loading = ref<boolean>(false);
const showDetail = ref<boolean>(false);
const isWait = ref<boolean>(false);
const changeLocation = ref(false);
const detailData = ref(null);
// const detailList = ref([]);
const processList = ref([]); //任务类型列表
const perationList = ref([]); //工序列表
const createTaskType = ref([
	//货物类型列表
	{
		text: 'WoodCarton60',
		value: 'WoodCarton60',
	},
	{
		text: 'SteelCarton60',
		value: 'SteelCarton60',
	},
	{
		text: 'SteelCarton48',
		value: 'SteelCarton48',
	},
	{
		text: 'WoodCarton48',
		value: 'WoodCarton48',
	},
]);
let tableList = ref([
	{
		id: 0,
		edit: false,
		seq: 0,
		code: '', //点位
		processCode: '', //工序code
		processName: '', //工序名称
		lineCode: '',
		lineName: '',
		locationCode: '',
		locationName: '',
		operation: 'COLLECT', //操作类型
		wareType: '', //货物类型
		currentQty: 0, //数量
		qty: 1, //目的地存在数量
		lineList: [], //产线列表
		locationList: [], //位置列表
	},
	{
		id: 1,
		edit: false,
		seq: 1,
		code: '',
		processCode: '',
		processName: '',
		lineCode: '',
		lineName: '',
		locationCode: '',
		locationName: '',
		operation: 'DELIVERY',
		wareType: '',
		currentQty: 0,
		qty: 1,
		lineList: [],
		locationList: [],
	},
]); //表格数据
// 提交的表单数据
const form = reactive({
	businessTaskType: '', //任务类型
	businessTaskTypeName: '', //任务名称
	initPriority: 0, //优先级
	cartonNo: '', //托盘号
	outputNo: '', //产出编号
	materialCode: '', //物料代码
	materialName: '', //物料名称
	scanType: '', //扫描类型
	cartonType: '', //货物类型
	createUserName: '', //用户名称
	lastModifyUserName: '',
	createBy: '', //用户工号
	createUserNo: '',
	lastModifyUserNo: '',
	listSteps: [],
});
const priorityList = reactive([
	//优先级列表
	{
		id: 1,
		name: 1,
	},
	{
		id: 2,
		name: 2,
	},
	{
		id: 3,
		name: 3,
	},
	{
		id: 4,
		name: 4,
	},
	{
		id: 5,
		name: 5,
	},
]);
const scanTypeList = reactive([
	//扫描类型列表
	{
		name: t('agv.noScan'),
		id: 'PICK',
	},
	{
		name: t('agv.putScan'),
		id: 'PUT',
	},
	{
		name: t('agv.pickScan'),
		id: 'NO',
	},
]);
// 表单校验规则
const dataRules = reactive({
	businessTaskType: [{ required: true, message: t('agv.taskTypeNotNull'), trigger: 'change' }],
	initPriority: [
		{
			required: true,
			message: t('agv.priorityNotNull'),
			trigger: 'change',
		},
	],
	scanType: [
		{
			required: true,
			message: t('agv.scanTypePlease'),
			trigger: 'change',
		},
	],
});
const dataRules2 = reactive({
	processCode: [{ required: true, message: t('agv.process'), trigger: 'change' }],
	lineCode: [{ required: true, message: t('agv.productionLine'), trigger: 'change' }],
	locationCode: [{ required: true, message: t('agv.position'), trigger: 'change' }],
	code: [{ required: true, message: t('agv.pointPosition'), trigger: 'blur' }],
	wareType: [{ required: true, message: t('agv.goodsType'), trigger: 'change' }],
});
const dataRules3 = reactive({
	processCode: [{ required: true, message: t('agv.process'), trigger: 'change' }],
	lineCode: [{ required: true, message: t('agv.productionLine'), trigger: 'change' }],
	locationCode: [{ required: true, message: t('agv.position'), trigger: 'change' }],
	code: [{ required: true, message: t('agv.pointPosition'), trigger: 'blur' }],
	wareType: [{ required: true, message: t('agv.goodsType'), trigger: 'change' }],
});
const submitForm = async (formEl, formEl2, formEl3) => {
	if (!formEl) return;
	if (!formEl2) return;
	if (!formEl3) return;
	let b = await formEl2.validate((valid) => {
		if (!valid) {
			return false;
		} else {
			return true;
		}
	});
	let c = await formEl3.validate((valid) => {
		if (!valid) {
			return false;
		} else {
			return true;
		}
	});
	let a = await formEl.validate((valid) => {
		if (!valid) {
			return false;
		} else {
			return true;
		}
	});
	if (a && b && c) {
		form.cartonType = tableList.value[0].wareType ? tableList.value[0].wareType : tableList.value[1].wareType;
		const queryForm = {
			businessTaskType: form.businessTaskType,
			businessTaskTypeName: form.businessTaskTypeName,
			initPriority: form.initPriority,
			cartonNo: form.cartonNo == '' ? null : form.cartonNo,
			outputNo: form.outputNo,
			scanType: form.scanType,
			cartonType: form.cartonType,
			listSteps: [],
		};
		queryForm.listSteps = tableList.value.map((item) => {
			return {
				code: item.code,
				processCode: item.processCode,
				lineCode: item.lineCode,
				locationCode: item.locationCode,
				seq: item.seq,
				operation: item.operation,
				wareType: item.wareType,
				qty: item.qty,
				currentQty: item.currentQty,
				processName: item.processName,
				lineName: item.lineName,
				locationName: item.locationName,
			};
		});
		createTask(queryForm).then((res) => {
			if (res.code === 400) {
				useMessage().warning(res.message);
				// useMessage().warning(t('agv.processNotNull'));
			} else if (res.code === 200) {
				useMessage().success(res.message);
				handleCancelClick();
			}
		});
	}
};
// 校验 状态的选择事件
const select = (e) => {
	form.selectId = e?.id;
};
// 获取任务类型列表
const getProcessList = () => {
	fetchTaskType({}).then((res: any) => {
		processList.value = res.result;
	});
};
// 任务类型选择
const getTaskTypeClick = (x) => {
	form.businessTaskType = x.itemValue;
	form.businessTaskTypeName = x.itemText;
};
// 获取工序 产线 位置列表
const getRuleList = (e: any, type: string) => {
	// if (!showDetail.value) {
	if (type === '1') {
		//获取工序
		if (perationList.value.length === 0) {
			fetchProcess({
				type: type,
				parentCode: '',
				scanType: '',
			}).then((res: any) => {
				perationList.value = res.result;
			});
		}
	} else if (type === '2') {
		if (tableList.value[e.seq].processCode !== '') {
			fetchProcess({
				type: type,
				parentCode: tableList.value[e.seq].processCode,
				scanType: '',
			}).then((res: any) => {
				tableList.value[e.seq].lineList = res.result;
			});
		}
	} else if (type === '3') {
		if (tableList.value[e.seq].lineCode !== '') {
			fetchProcess({
				type: type,
				parentCode: tableList.value[e.seq].lineCode,
				scanType: '',
			}).then((res: any) => {
				tableList.value[e.seq].locationList = res.result;
			});
		}
	}
};
const getRuleClick = (e: any, type?: string) => {
	// if (!showDetail.value) {
	perationList.value.forEach((item) => {
		if (item.code === e.processCode) {
			tableList.value[e.id].processName = item.text;
		}
	});
	tableList.value[e.id].lineList.forEach((item) => {
		if (item.code === e.lineCode) {
			tableList.value[e.id].lineName = item.text;
		}
	});
	if (type === '3') {
		let index = tableList.value[e.id].locationList.findIndex((item) => item.code === e.locationCode);
		tableList.value[e.id].locationName = tableList.value[e.id].locationList[index].text;
		tableList.value[e.id].code = e.locationList[index]?.point;
		tableList.value[e.id].wareType = e.locationList[index]?.cartonType;
		tableList.value[e.id].currentQty = e.locationList[index]?.qty;
		if (e.id == 0) {
			tableList.value[e.id].operation = 'COLLECT';
		} else if (e.id == 1) {
			tableList.value[e.id].operation = 'DELIVERY';
		}
	}
	// }
};
//点击提交
const handleCreateClick = () => {
	submitForm(dataFormRef.value, dataFormRef2.value, dataFormRef3.value);
};
// 取消任务
const handleCancel = () => {
	let parmes = {
		taskCode: detailData.value.taskCode,
	};
	cancelTaskByTaskCode(parmes).then((res) => {
		if (res.code == '400') {
			useMessage().warning(res.message);
		} else if (res.code == '200') {
			useMessage().success(t('agv.cancelSucceeded'));
			handleCancelClick();
			getdataList();
		}
	});
};
// 修改优先级
const handleChangePriority = () => {
	changePriorityRef.value.openDialog(detailData.value);
};
const handleLocation = () => {
	let index = tableList.value.findIndex((item) => item.operation == 'DELIVERY');
	tableList.value[index].edit = true;
	changeLocation.value = true;
};
const handleChange = () => {
	let parmes = {
		taskCode: detailData.value.taskCode,
		taskstep: tableList.value[1],
	};
	modifyTaskLocation(parmes).then((res) => {
		if (res.code == 200) {
			useMessage().success(t('agv.modifySuccessfully'));
		} else {
			useMessage().error(res.message);
		}
		tableList.value[1].edit = false;
		changeLocation.value = false;
	});
};
// 关闭弹框
const handleCancelClick = () => {
	tableList.value = [
		{
			id: 0,
			edit: false,
			seq: 0,
			code: '', //点位
			processCode: '', //工序code
			processName: '', //工序名称
			lineCode: '',
			lineName: '',
			locationCode: '',
			locationName: '',
			operation: '', //操作类型
			wareType: '', //货物类型
			currentQty: 0, //数量
			qty: 1, //目的地存在数量
			lineList: [], //产线列表
			locationList: [], //位置列表
		},
		{
			id: 1,
			edit: false,
			seq: 1,
			code: '',
			processCode: '',
			processName: '',
			lineCode: '',
			lineName: '',
			locationCode: '',
			locationName: '',
			operation: '',
			wareType: '',
			currentQty: 0,
			qty: 1,
			lineList: [],
			locationList: [],
		},
	];
	tableList.value[1].edit = false;
	dataFormRef.value?.resetFields();
	// detailList.value = [];
	isWait.value = false;
	visible.value = false;
	changeLocation.value = false;
};
// 打开弹框
const openAddDialog = (e?: any) => {
	visible.value = true;
	showDetail.value = false;
	if (e) {
		showDetail.value = true;
		detailData.value = e;
		let obj = {
			taskCode: e.taskCode,
			lang: i18n.global.locale.value,
		};
		fetchTaskDetail(obj).then((res) => {
			tableList.value.forEach((listItem) => {
				res.result.forEach((detailItem) => {
					if (listItem.seq === detailItem.seq) {
						listItem.code = detailItem.code;
						listItem.processCode = detailItem.processCode;
						listItem.processName = detailItem.processName;
						listItem.lineCode = detailItem.lineCode;
						listItem.lineName = detailItem.lineName;
						listItem.locationCode = detailItem.locationCode;
						listItem.locationName = detailItem.locationName;
						listItem.operation = detailItem.operation;
						listItem.wareType = detailItem.wareType;
						listItem.currentQty = detailItem.currentQty;
						listItem.qty = detailItem.qty;
					}
				});
			});
		});
	}
	if (e?.taskStatusStr === '等待步骤执行') {
		isWait.value = true;
	}
};
// 暴露变量
defineExpose({
	openAddDialog,
});
</script>
<style scoped lang="scss">
.box-card {
	padding-left: 50px;
	margin-bottom: 30px;

	.el-row {
		height: 30px;
		line-height: 30px;
	}
}
</style>
